//
//  ZZTableViewController.m
//  zhengzai-tv
//
//  Created by SC on 16/6/17.
//  Copyright © 2016年 zhengzai.tv. All rights reserved.
//

#import "ZZTableViewController.h"
#import "ZZTableViewModel.h"
#import "RZDataBinding.h"
#import "MJRefresh.h"

@interface ZZTableViewController ()

@property (nonatomic, strong, readwrite) UITableView *tableView;
@property (nonatomic, strong) ZZTableViewModel *viewModel;

@end

@implementation ZZTableViewController

@synthesize viewModel = _viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.contentInset  = self.contentInset;
    self.tableView.scrollIndicatorInsets = self.contentInset;
    
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.viewModel.ownerTableView = self.tableView;
    
    if ([self.viewModel shouldRequestRemoteDataOnViewDidLoad]) {
        [self.viewModel requestRemoteData];
    }
    self.tableView.tableFooterView = [UIView new];
}

- (void)bindViewModel
{
    [super bindViewModel];
    [self.viewModel rz_addTarget:self action:@selector(reloadData:) forKeyPathChange:@"dataSource"];
    [self.viewModel rz_addTarget:self action:@selector(pullToReload:) forKeyPathChange:@"shouldPullToRefresh"];
    [self.viewModel rz_addTarget:self action:@selector(infiniteScrolling:) forKeyPathChange:@"shouldInfiniteScrolling"];
}

- (void)reloadData:(id)change
{
    [self.viewModel endRefresh];
    [self.tableView reloadData];
}

- (void)infiniteScrolling:(id)change
{
    if ([[change objectForKey:kRZDBChangeKeyNew] boolValue]) {
        __weak typeof(self) weakSelf = self;
        self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakSelf.viewModel requestRemoteDataWithPage:self.viewModel.page + 1];
        }];
    }
}

- (void)pullToReload:(id)change
{
    if ([[change objectForKey:kRZDBChangeKeyNew] boolValue]) {
        __weak typeof(self) weakSelf = self;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf.viewModel requestRemoteDataWithPage:1];
        }];
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.viewModel.dataSource ? self.viewModel.dataSource.count : 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.viewModel.dataSource[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.viewModel didSelectRowAtIndexPath:indexPath];
}

#pragma mark - DZNEmptyDataSetSource
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    return [[NSAttributedString alloc] initWithString:@"No Data"];
}

#pragma mark - DZNEmptyDataSetDelegate
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return self.viewModel.dataSource == nil;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

- (CGPoint)offsetForEmptyDataSet:(UIScrollView *)scrollView {
    return CGPointMake(0, -(self.tableView.contentInset.top - self.tableView.contentInset.bottom) / 2);
}

#pragma mark - 懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
@end
